<?php
/*
**	pagination 		文件上传
**	@Author			Alan
**	@Support		alanyao80@gmail.com
*	@Finish			2009-03-12
*/

class MyUpload{
	public $up_file=array();												//表单确认上传的文件
	public $up_file_type=array();											//表单确认上传的文件类型
	public $output_file=array();											//上传后存入地址
	public $output_file2=array();											//上传后缩略图地址
	public $config=array(													//参数设置
		'filename'  => array(),												//新文件名,如空为随机
		'maxsize'   => 1024, 												//最大文件大小 k为单位
		'type'      => array("jpg","gif","png"), 							//允许类型
		'path'      => '',													//文件存入目录
		'thumb_path'=> 'thumb/',											//缩略图目录
		'upload_root'=> UPLOAD_PHOTO,										//上传根目录
		'datepath'  => true,												//是否用日期分目录
		'date'    	=> ''													//日期目录方式,不可设置
	 );
	
	public function __construct($config=''){	
		if($config){
			if(!is_array($config))
			die('设置的参数必须为数组');
		
			$default = $this->config;
			foreach ($config as $k => $v){
				if(!array_key_exists($k, $default)) 
				die('不存在此参数类别');
				$default[$k] = $v; 
			}
			$this->config = $default;
			
		}
	}
	
	/**
	 * 检查表单上的文件内容
	 * @param string $key
	 * @return bool/array
	 */
	public function check_form($key){
		if($_FILES[$key] && isset($_FILES[$key]['error'])){
			$count = 0;
			//Myfunction::print_data($_FILES[$key]);
			
			if(is_array($_FILES[$key]['tmp_name'])){
				foreach($_FILES[$key]['tmp_name'] as $k=>$v){
					if($_FILES[$key]['error'][$k]=='0' && $_FILES[$key]['tmp_name'][$k]){
						$this->up_file['tmp_name'][] = $_FILES[$key]['tmp_name'][$k];
						$this->up_file['size'][]= $_FILES[$key]['size'][$k];
						$this->up_file['type'][]= $this->get_type($_FILES[$key]['name'][$k]);
						$count++;	
					}
				}
			}else{
				if($_FILES[$key]['error']=='0' && $_FILES[$key]['tmp_name']){
					$this->up_file['tmp_name'][0] = $_FILES[$key]['tmp_name'];
					$this->up_file['size'][0]= $_FILES[$key]['size'];
					$this->up_file['type'][0]= $this->get_type($_FILES[$key]['name']);
					$count++;
				}
			}
			if($count)
			return $this->up_file;													//确认上传的文件
			else
			return false;
		}
		else
		return false;
		
	}
	
	/**
	 * 路径是否存在可写,同时生成日期目录
	 * @return bool
	 */
	public function check_path(){
		$path = $this->config['upload_root'].$this->config['path']; 
		$thumb_path = $this->config['upload_root'].$this->config['path'].$this->config['thumb_path']; 
		//Myfunction::print_data($path);

		if(!is_dir($path)){ 
			mkdir($path,0777);
			//die('上传目录不存在');
		   	//return false; 
		}
		if(!is_writable($path)){
			die('上传目录不可写');
		   	return false; 
		}
		
		$type = $this->config['datepath']; 										//是否使用日期方式目录
		
		if($type){
			//$path1 = $path.date("Y").'/';
			$path_format = date("Y").date("m").date("d").'/';
			$image_path =$path.$path_format;
			$thumb_path = $thumb_path.$path_format;
			//echo "1".$image_path."2";
			//exit;
			//if(!is_dir($path1))													//大图目录
			//@mkdir($path1,0777);
			if(!is_dir($image_path))
			@mkdir($image_path,0777);

			if(!is_dir($thumb_path))											//缩略图目录
			@mkdir($thumb_path,0777);

		
			$this->config['date'] = $path_format;
			$this->config['path'] = $path;
		}else{
			$this->config['date'] = '';
			$this->config['path'] = $path;
		}
		
		return true; 
	}
	
	/**
	 * 判断文件是否超过最大限度
	 * @param string $key
	 * @return bool
	 */
	public function check_size($key){
		$config_size = $this->config['maxsize']*1024; 							// 转换成字节 
		
		foreach($this->up_file['size'] as $size){ 
		   	if($size > $config_size){ 
				die('文件大小最大限制为: <font color="red">'.$this->config['maxsize'].'K</font>');
			   	return false; 
		   	} 
		}   
		return true; 
	}
	
	/**
	 * 判断文件类型是否允许
	 * @param string $key
	 * @return bool
	 */
	public function check_type($key){
		$type_array = $this->config['type']; 									// 允许上传文件类型数组 
		 							
		// 上传的文件类型校验 

		foreach($this->up_file['type'] as $type) { 
			if(!in_array($type, $type_array)){ 									// 不允许类型 
				die('只允许文件格式为: <font color="red">'.  implode(',', $type_array).'</font>'); 
			   	return false; 
			} 
		} 
		return true; 
	}
	
	/**
	 * 获取文件类型
	 * @param string $file
	 * @return string
	 */
	public function get_type($file){
            return strtolower(substr(strrchr( $file, "." ),1));
            //return $this->up_file['type'];
	}

	public function checkimage(){
		$type = $this->get_type($this->output_file[0]);
		if(in_array($type,array("jpg","jpeg","gif","png","bmp"))){
			return true;	
		}else{
			return false;	
		}
	}
        /**
	 * 获取文件类型
	 * @return array
	 */
	public function output_type(){
            return $this->up_file['type'];
	}

	/**
	 * 获取文件随机名
	 * @param int $file
	 * @return string
	 */
	public function get_rand_name($length=4){
		srand();
		$possible_charactors = "abcdefghjkmnpqrstuvwxyz23456789";
		$string = "";
		while(strlen($string)<$length) {
			$string .= substr($possible_charactors,(rand()%(strlen($possible_charactors))),1);
		}
		return date("ymdHis").$string;
	}
	
	/**
	 * 获取刚上传的文件全地址
	 * @return string
	 */
	public function get_output_file(){
		return $this->output_file;
	}
	
	/**
	 * 文件上传,返回上传后的文件名
	 * @param string $key
	 * @return string
	 */
	public function upload($key){
		$this->check_form($key);
		$this->check_path();
		$this->check_type($key);
		$this->check_size($key);
		
		
		$name_array = $this->config['filename'];

		$file_count = count($this->up_file['tmp_name']);
		$return = array();
		
		for($i=0;$i<$file_count;$i++){
			if($name_array[$i]){													//指定新文件名
				$new_file = $name_array[$i].'.'.$this->up_file['type'][$i];
			}else{
				$rand = $this->get_rand_name();										//不指定时为随机
				$new_file = $rand.'.'.$this->up_file['type'][$i];
			}
			
			$output = $this->config['path'].$this->config['date'].$new_file;
			
			if(!@move_uploaded_file($this->up_file['tmp_name'][$i],$output)){
			    die('上传中发生错误');
			}
			
			$this->output_file[] = $output;											//全路径文件
			$this->output_file2[] = $this->config['path'].$this->config['thumb_path'].$this->config['date'].$new_file;						//缩略图文件名
			$return[] =  $this->config['date'].$new_file;									//不带目录的文件
			
		}
		return $return;
	}
	
	/***缩略图文件名***/
	public function get_thumb_name(){
		return $this->output_file2;
	}
	
}


/*example

$myupload = new MyUpload(array('filename'=>array('aaa'),'maxsize'=>'100'));
if($myupload->check_form('upfile')){
	$output = $myupload->upload('upfile');
	$source = $myupload->get_output_file();
}else{
	echo "noform";	
}

*/

?>
